<template>
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户账号" prop="mobile">
        <el-input v-model.trim="queryParams.mobile" @keyup.enter="getList" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="用户名称" prop="nickName">
        <el-input v-model.trim="queryParams.nickName" @keyup.enter="getList" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="&nbsp;">
        <el-button @click="handleSearch(false)">重置</el-button>
        <el-button type="primary" @click="handleSearch(true)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" width="50" type="index" :index="indexAdd" align="center" />
      <el-table-column label="用户账号" align="center" prop="mobile" />
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="体重 kg" align="center" prop="weight" />
      <el-table-column label="身高 cm" align="center" prop="height" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="日均热量摄入 kcal" align="center" prop="averageKcal" />
      <el-table-column label="基础代谢" align="center" prop="bmr" />
      <el-table-column label="详情" align="center" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDetails(row)"> 饮食详情 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <dialogDetails ref="dialogRef" />
  </div>
</template>
<script setup>
import { getHealthListApi } from '@/api/nutrition/userCalories'
import dialogDetails from './components/details'

const dialogRef = ref()
const queryRef = ref()
const showSearch = ref(true)
const loading = ref(false)
const total = ref(0)
const list = ref([])
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  mobile: null,
  nickName: null
})
/**
 *   页面切换递增
 * @param {*} index
 */
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams.value
  return index + 1 + (pageNum - 1) * pageSize
}
function handleSearch(type) {
  !type && queryRef.value.resetFields()
  getList()
}
/**详情 */
function handleDetails(row) {
  dialogRef.value.init(row)
}
/**查询列表 */
function getList() {
  loading.value = true
  getHealthListApi(queryParams.value)
    .then((res) => {
      total.value = res.total || 0
      list.value = res.rows || []
    })
    .finally(() => {
      loading.value = false
    })
}

getList()
</script>
<style lang="scss" scoped></style>
